//
//  BooksPushViewController.m
//  Cartoon
//
//  Created by dllo on 15/10/28.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "BooksPushViewController.h"
#import "AnimationCollectionViewCell.h"
#import "AnimationModel.h"
#import "DetailModel.h"
#import "LookViewController.h"

@interface BooksPushViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong) UICollectionView *collectionView;

@property (nonatomic,strong) UIView *backView;

@property (nonatomic,strong) UILabel *detailLabel;

@property (nonatomic,strong) UIButton *button;

@property (nonatomic,strong) UIButton *startButton;

@property (nonatomic,strong) UIButton *saveButton;

@end

@implementation BooksPushViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self getValuesForInternet];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:241/255.0 alpha:1];
    
    NSLog(@"漫画的ID是:%@",self.booksId);
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(60, 30);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flowLayout.minimumLineSpacing = 20;
    flowLayout.minimumLineSpacing = 20;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height / 2 + 35, self.view.bounds.size.width, self.view.bounds.size.height / 2.5 - 20) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[AnimationCollectionViewCell class] forCellWithReuseIdentifier:@"InsidePageReuse"];
    [self.view addSubview:self.collectionView];
    
    
    
    
    
}


- (void)getValuesForInternet
{
    NSString *str = [NSString stringWithFormat:@"http://api.dmgezi.com/comic/books/%@.json",self.booksId];
    
    str = [str stringByAddingPercentEncodingWithAllowedCharacters:[ NSCharacterSet URLFragmentAllowedCharacterSet]];
    
    self.pageDic = [NSMutableDictionary dictionary];
    self.detailArray = [NSMutableArray array];
    self.booksArray = [NSMutableArray array];
    
    [NetHandler dataWithUrl:str complant:^(NSData *data) {
        
        self.pageDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        self.detailArray = [NSMutableArray array];
                            
        self.detailArray = [self.pageDic objectForKey:@"properties"];
        

        NSMutableArray *array = [self.pageDic objectForKey:@"chapters"];
        
        for (NSMutableDictionary *dic in array) {
            
            AnimationModel *AnimaModel = [[AnimationModel alloc]init];
            
            [AnimaModel setValuesForKeysWithDictionary:dic];
            
            [self.booksArray addObject:AnimaModel];
            
            
        }
        
        self.title = [self.pageDic objectForKey:@"name"];
        
        
        UIImageView *upBackView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 2.8 + 28)];
        [upBackView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@",[self.pageDic objectForKey:@"cover"]]]];
        
        upBackView.backgroundColor = [UIColor cyanColor];
        [self.view addSubview:upBackView];
        
        
        UIVisualEffectView *mohu =[[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
        mohu.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 2.8 + 2 + 28);
        mohu.alpha = 1;
        [upBackView addSubview:mohu];
        
        UIView *blackLine = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height / 2.8 + 30, self.view.bounds.size.width, 0.5)];
        blackLine.alpha = 0.6;
        blackLine.backgroundColor = [UIColor grayColor];
        [mohu addSubview:blackLine];
        
        
        UIImageView *leftPic = [[UIImageView alloc]initWithFrame:CGRectMake(10, 70, 120, 160)];
        leftPic.clipsToBounds = YES;
        leftPic.layer.cornerRadius = 15;
        [leftPic sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@",[self.pageDic objectForKey:@"cover"]]]];
        [self.view addSubview:leftPic];
        

        
        self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height / 2.8 + 10 + 28, self.view.bounds.size.width, self.view.bounds.size.height / 7 + 10)];
        self.backView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:self.backView];
        
        self.detailLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, self.view.bounds.size.width - 15, self.view.bounds.size.height / 4)];
        self.detailLabel.numberOfLines = 4;
        self.detailLabel.font = [UIFont systemFontOfSize:15];
        self.detailLabel.textColor = [UIColor blackColor];
        self.detailLabel.text = [self.pageDic objectForKey:@"description"];
        [self.detailLabel sizeToFit];
        [self.backView  addSubview:self.detailLabel];
        
        
        self.button = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.button setTitle:@"展开∨" forState:normal];
        self.button.titleLabel.font = [UIFont systemFontOfSize:13];
        [self.button setTitleColor:[UIColor colorWithRed:149/255.0 green:198/255.0 blue:64/255.0 alpha:1] forState:normal];
        self.button.frame = CGRectMake(self.view.bounds.size.width / 10 * 9 - 10, 75, 40, 20);
        [self.backView  addSubview:self.button];
        self.button.tag = 99;
        
        [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        self.startButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.startButton setTitle:@"开始阅读" forState:normal];
        self.startButton.titleLabel.font = [UIFont systemFontOfSize: 18];
        self.startButton.backgroundColor = [UIColor colorWithRed:149/255.0 green:198/255.0 blue:64/255.0 alpha:1];
        self.startButton.layer.cornerRadius = 4;
        self.startButton.frame = CGRectMake(self.view.bounds.size.width / 1.4, self.view.bounds.size.height / 4 + 30, 100, 40);
        [self.startButton setTitleColor:[UIColor whiteColor] forState:normal];
        [self.view addSubview:self.startButton];
        
        
        self.saveButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.saveButton setTitle:@"收藏" forState:normal];
        self.saveButton.layer.cornerRadius = 4;
        [self.saveButton setTitleColor:[UIColor whiteColor] forState:normal];
        self.saveButton.titleLabel.font = [UIFont systemFontOfSize:18];
        self.saveButton.backgroundColor = [UIColor colorWithRed:288/255.0 green:85/255.0 blue:33/255.0 alpha:1];
        self.saveButton.frame = CGRectMake(self.view.bounds.size.width / 2.5, self.view.bounds.size.height / 4 + 30, 100, 40);
        [self.view addSubview:self.saveButton];
        
        
          [self.collectionView reloadData];
        
    }];
    
  
    
    
    
}

- (void)buttonAction:(UIButton *)but
{
    if (self.button.tag == 99) {
        [UIView animateWithDuration:0.5 animations:^{
            self.backView.frame = CGRectMake(0, self.view.bounds.size.height / 2.8 + 10 + 28, self.view.bounds.size.width, self.view.bounds.size.height);
            self.detailLabel.numberOfLines = 0;
            self.button.frame = CGRectMake(self.view.bounds.size.width / 10 * 9 - 10, 330, 40, 20);
            [self.button setTitle:@"合起∧" forState:normal];
            [self.detailLabel sizeToFit];
        }];
        self.button.tag = 100;
    }else if (self.button.tag == 100){
        
        [UIView animateWithDuration:0.5 animations:^{
        self.button.tag = 99;
        [self.button setTitle:@"展开∨" forState:normal];
        self.button.frame = CGRectMake(self.view.bounds.size.width / 10 * 9 - 10, 75, 40, 20);
        self.detailLabel.numberOfLines = 4;
        [self.detailLabel sizeToFit];
        self.backView.frame = CGRectMake(0, self.view.bounds.size.height / 2.8 + 10 + 28, self.view.bounds.size.width, self.view.bounds.size.height / 7 + 10);
         }];
    }
    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSLog(@"漫画共有:%ld卷",self.booksArray.count);
    
    return self.booksArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AnimationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"InsidePageReuse" forIndexPath:indexPath];
    
    AnimationModel *model = [self.booksArray objectAtIndex:indexPath.item];
    
    cell.numberLabel.text = model.name;
    
    return cell;
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LookViewController *lookVC = [[LookViewController alloc]init];
    AnimationModel *model = [self.booksArray objectAtIndex:indexPath.item];
    
    lookVC.cartoonId = model.cartoonId;
    [self presentViewController:lookVC animated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
